﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using MRG.Controls.UI;
using System.Collections.Specialized;
using FreeBroker.Properties;
using System.ComponentModel;
using System.Configuration;
using System.IO;
using System.Collections;

namespace FreeBroker
{
    public class BrokerObject : ComDirectSearchResult
    {
        public BrokerObject()
        {
            // Create standard options here
            chartOptionIndices = new Dictionary<string, int>();
            chartOptionIndices.Add(ChartController.OPT_TIMEPERIOD, 0);
            chartOptionIndices.Add(ChartController.OPT_CHARTTYPE, 0);
            chartOptionIndices.Add(ChartController.OPT_CHARTCALC, 0);
            chartOptionIndices.Add(ChartController.OPT_CHARTVALUE, 0);
            chartOptionIndices.Add(ChartController.OPT_AVERAGETYPE, 0);
            buildChartOptionDict();
        }

        public BrokerObject(GroupBox assignedGroupBox)
        {
            // Create standard options here
            chartOptionIndices = new Dictionary<string, int>();
            chartOptionIndices.Add(ChartController.OPT_TIMEPERIOD, 0);
            chartOptionIndices.Add(ChartController.OPT_CHARTTYPE, 0);
            chartOptionIndices.Add(ChartController.OPT_CHARTCALC, 0);
            chartOptionIndices.Add(ChartController.OPT_CHARTVALUE, 0);
            chartOptionIndices.Add(ChartController.OPT_AVERAGETYPE, 0);
            buildChartOptionDict();

            this.assignedGroupBox = assignedGroupBox;
            ComboBox combo = null;
            PictureBox picBox = null;
            LoadingCircle circle = null;
            foreach (Control control in assignedGroupBox.Controls)
            {
                if (control.GetType() == typeof(ComboBox))
                {
                    combo = (ComboBox)control;
                }
                else if (control.GetType() == typeof(PictureBox))
                {
                    picBox = (PictureBox)control;
                }
                else if (control.GetType() == typeof(LoadingCircle))
                {
                    circle = (LoadingCircle)control;
                }
            }
            this.assignedPictureBox = picBox;
            this.assignedLoadingCircle = circle;
            this.assignedComboBox = combo;
        }

        private Image thumbnail;
        public Image Thumbnail
        {
            set { this.thumbnail = value; }
            get 
            {
                //if (this.thumbnail == null)
                //{
                //    this.thumbnail = new Communication().comdirectImage("http://isht.comdirect.de/charts/small.ewf.chart?hist=1y&sSym=" + Symbol, "");
                //}
                return this.thumbnail; 
            }
        }

        private Image chart;
        public Image Chart
        {
            set { this.chart = value; }
            get { return this.chart; }
        }

        private BackgroundWorker worker;
        public BackgroundWorker Worker
        {
            set { this.worker = value; }
            get { return this.worker; }
        }

        private string idNotation;
        public string IdNotation
        {
            set { this.idNotation = value; }
            get { return this.idNotation; }
        }

        private Dictionary<string, Color> additionalNotations = new Dictionary<string, Color>();
        public Dictionary<string, Color> AdditionalNotations
        {
            set { this.additionalNotations = value; }
            get { return this.additionalNotations; }
        }

        private string isin;
        public string Isin
        {
            set { this.isin = value; }
            get { return this.isin; }
        }

        private string wkn;
        public string Wkn
        {
            set { this.wkn = value; }
            get { return this.wkn; }
        }

        private string symbol;
        public string Symbol
        {
            set { this.symbol = value; }
            get { return this.symbol; }
        }

        private string country;
        public string Country
        {
            set { this.country = value; }
            get { return this.country; }
        }

        private bool active;
        public bool Active
        {
            get { return active; }
            set { active = value; }
        }

        public Image chartThumbnail;
        public Image chartDetail;
        public Dictionary<string, string> chartOptions;
        public Dictionary<string, int> chartOptionIndices;

        private void buildChartOptionDict()
        {
            if (this.chartOptionIndices != null)
            {
                this.chartOptions = new Dictionary<string, string>();
                foreach (string item in this.chartOptionIndices.Keys)
                {
                    int index = this.chartOptionIndices[item];
                    if (index != -1)
                    {
                        string value = ChartController.OPTIONS[item].ElementAt(index).Value;
                        this.chartOptions.Add(item, value);
                    }
                }
            }
        }

        public void saveChartOptions(int number)
        {
            if (this.chartOptions != null)
            {
                List<int> col = new List<int>();
                
                foreach (string item in ChartController.OPTIONS.Keys)
                {
                    if (this.chartOptionIndices.ContainsKey(item))
                    {
                        // What was selected, e.g. 0, 1...
                        col.Add(this.chartOptionIndices[item]);
                    }
                    else
                    {
                        // Nothing selected
                        col.Add(-1);
                    }
                }

                ChartOptions newChartOptions = Config.Broker.GetChartOptionsByNumber(number);
                if (newChartOptions == null)
                {
                    newChartOptions = new ChartOptions();
                    newChartOptions.Number = number;
                    Config.Broker.BrokerCharts.Add(newChartOptions);
                }
                newChartOptions.SetOptions(col);

                //setCol(number, col);
                //Settings.Default.Save();
                this.buildChartOptionDict();
            }
        }

        public void loadChartOptions(int number)
        {
            ChartOptions col = Config.Broker.GetChartOptionsByNumber(number);
            if (col == null)
            {
                // Check if standard options have been configured
                col = Config.Broker.GetStandardChartOptions(number);
            }

            if (col != null)
            {
                this.chartOptionIndices = new Dictionary<string, int>();
                try
                {
                    int index = 0;
                    foreach (string item in ChartController.OPTIONS.Keys)
                    {
                        int val = (int)col.Options[index];
                        this.chartOptionIndices.Add(item, val);
                        index++;
                    }
                    this.buildChartOptionDict();
                }
                catch (Exception e)
                {
                    Console.WriteLine("ERROR: " + e.Message);
                }
            }
        }

        private int brokerIndex;
        public int BrokerIndex
        {
            set { this.brokerIndex = value; }
            get { return this.brokerIndex; }
        }

        private bool automaticJob;
        public bool AutomaticJob
        {
            set { this.automaticJob = value; }
            get { return this.automaticJob; }
        }

        private Form activeDetailView;
        public Form ActiveDetailView
        {
            set { this.activeDetailView = value; }
            get { return this.activeDetailView; }
        }

        private GroupBox assignedGroupBox;
        public GroupBox AssignedGroupBox
        {
            set { this.assignedGroupBox = value; }
            get { return this.assignedGroupBox; }
        }

        private PictureBox assignedPictureBox;
        public PictureBox AssignedPictureBox
        {
            set { this.assignedPictureBox = value; }
            get { return this.assignedPictureBox; }
        }

        private LoadingCircle assignedLoadingCircle;
        public LoadingCircle AssignedLoadingCircle
        {
            set { this.assignedLoadingCircle = value; }
            get { return this.assignedLoadingCircle; }
        }

        private ComboBox assignedComboBox;
        public ComboBox AssignedComboBox
        {
            set { this.assignedComboBox = value; }
            get { return this.assignedComboBox; }
        }

        public string GetUnescapedName()
        {
            string unescaped = Helper.UnescapeHtmlString(Name);
            // Take care of the & that is used for shortcut keys in GUI
            unescaped = unescaped.Replace(" & ", " && ");
            return unescaped;
        }
    }
}
